// pages/compony-pages/compony-address/compony-address.js
let sourceArea = [];
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        areaArr: [
            ['北京市'],
            ["东城区", "西城区", "朝阳区", "丰台区", "石景山区", "海淀区", "门头沟区", "房山区", "通州区", "顺义区", "昌平区", "大兴区", "怀柔区", "平谷区", "密云区", "延庆区"],
            ['']
        ],
        areaIndexs: [0, 0, 0],
        selectArea:['' , '' , ''],
        addressDetail:''
    },
    /**
     * -------------------网络请求部分-------------------
     */
    editEnterpriseAddress(data){
        wx.showLoading({
            title: '加载中...',
            mask: true
        })
        wx.request({
            url: app.globalData.serverAddress.editEnterpriseAddress,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'POST',
            success: function(res) {
                console.log("editEnterpriseAddress success" , res);
                if (res.statusCode == 200 && res.data.CodeStatus){
                    app.globalData.enterpriseInfo = res.data.BodyContent;
                    wx.navigateBack({
                        delta: 1
                    });
                }
            },
            fail: function (res) { console.log("editEnterpriseAddress fail" , res);},
            complete: function(res) {wx.hideLoading();},
        })
    },
    getAreaList() {
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        let that = this;
        wx.request({
            url: 'https://www.51xj.mobi/city.txt',
            header: {},
            method: 'GET',
            success: function(res) {
                if (res.statusCode == 200) {
                    console.log(res);
                    sourceArea = res.data;
                    that.covertToArea(res.data);
                }
            },
            complete: function(res) {
                wx.hideLoading();
            },
        })
    },
    covertToArea(data) {
        let firstArea = [];
        let secondArea = [];
        let thirdArea = [''];
        //获取一级联动数据
        for(let i = 0; i < data.length; i++){
            firstArea.push(data[i].name);
        }
        for (let i = 0; i < data[0].city.length; i++) {
            secondArea.push(data[0].city[i].name);
        }
        //三级联动数据
        thirdArea = data[0].city[0].area;
        let _data = {
            areaArr: this.data.areaArr
        };
        _data.areaArr[0] = firstArea;
        _data.areaArr[1] = secondArea;
        _data.areaArr[2] = thirdArea;
        this.setData(_data);
    },
    /**
     * -------------------网络请求部分-------------------
     */

    /**
     * ===================事件处理部分===================
     */
    areaPickerChange(e) {
        let data = {
            selectArea: this.data.selectArea,
            areaIndexs: this.data.areaIndexs
        };
        data.selectArea = [];
        data.selectArea.push(this.data.areaArr[0][data.areaIndexs[0]]);
        data.selectArea.push(this.data.areaArr[1][data.areaIndexs[1]]);
        data.selectArea.push(this.data.areaArr[2][data.areaIndexs[2]]);
        this.setData(data);
    },
    areaItemPickerChange(e) {
        // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
        let data = {
            areaIndexs: this.data.areaIndexs
        };
        switch(e.detail.column){
            case 0:
                this.setSecondAndThird(e.detail.value);
                data.areaIndexs[0] = e.detail.value;
                break;
            case 1:
                this.setThirdArea(e.detail.value);
                data.areaIndexs[1] = e.detail.value;
                break;
            case 2:
                data.areaIndexs[2] = e.detail.value;
                break;
        }
        this.setData(data);
    },
    setSecondAndThird(index){
        let secondArea = [];
        let thirdArea = [''];
        //设置二级联动数据
        for (let i = 0; i < sourceArea[index].city.length; i++) {
            secondArea.push(sourceArea[index].city[i].name);
        }
        //三级联动数据
        thirdArea = sourceArea[index].city[0].area;
        let _data = {
            areaArr: this.data.areaArr
        };
        // console.log(secondArea , thirdArea);
        _data.areaArr[1] = secondArea;
        _data.areaArr[2] = thirdArea;
        this.setData(_data);
    },
    setThirdArea(index){
        let firstIndex = this.data.areaIndexs[0];
        if (sourceArea[firstIndex].city.length == 1){
            return;
        }
        let thirdArea = [];
        let data = {
            areaArr: this.data.areaArr
        };
        thirdArea = sourceArea[firstIndex].city[index].area;
        data.areaArr[2] = thirdArea;
        this.setData(data);
    },
    clickSave(){
        let selectAddr = this.data.selectArea;
        let address = selectAddr[0] + selectAddr[1] + selectAddr[2];
        console.log(address);
        if(address){
            let data = {
                Id: app.globalData.enterpriseInfo.Id,
                Province: selectAddr[0],
                City: selectAddr[1],
                District: selectAddr[2],
                Address: this.data.addressDetail
            };
            this.editEnterpriseAddress(data);
        }
    },
    inputAddress(e){
        this.setData({
            addressDetail: e.detail.value
        });
    },
    /**
     * ===================事件处理部分===================
     */

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getAreaList();
        let data = {
            selectArea:[],
            addressDetail:''
        };
        if (app.globalData.enterpriseInfo.Province){
            data.selectArea.push(app.globalData.enterpriseInfo.Province);
            data.selectArea.push(app.globalData.enterpriseInfo.City);
            data.selectArea.push(app.globalData.enterpriseInfo.District);
        }
        if (app.globalData.enterpriseInfo.Address){
            data.addressDetail = app.globalData.enterpriseInfo.Address;
        }
        this.setData(data);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})